// pages/user/mine.js
import {
  $wuxDialog
} from '../../miniprogram_npm/wux-weapp/index';
import {
  getMemberInfo
} from '../../api/user';
import {
  formatPrice
} from '../../utils/util';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    isIOS: false,
    isApply:false,
    showContent:'',
    avatarUrl: '',
    nickName: '',
    companyName: '',
    mobile: '',
    vip: false,
    roleName:'普通用户',
    roleId:0,
    vipPriceMonth: 0,
    vipDeadline: 0,
    navList: [{
      icon: '/resource/images/icon_mine_order.png',
      text: '我的订单',
      url: '/pages/user/order/list/list',
      badge: false
    }, {
      icon: '/resource/images/icon_mine_activity.png',
      text: '我的活动',
      url: '/pages/user/activity/activity',
      badge: false
    }, {
      icon: '/resource/images/icon_mine_certificate.png',
      text: '我的证书',
      url: '/pages/credentials/user/user',
      badge: true
    }, {
      icon: '/resource/images/icon_mine_college.png',
      text: '我的企业学堂',
      url: '/pages/user/businessSchool/details',
      badge: true
    }, 
    // {
    //   icon: '/resource/images/icon_mine_enterprise.png',
    //   text: '绑定企业',
    //   url: '/pages/user/school/bind/company',
    //   badge: true
    // },
    {
      icon: '/resource/images/icon_mine_enterprise.png',
      text: '我的学校',
      url: '/pages/user/school/details',
      badge: true
    }, {
      icon: '/resource/images/icon_mine_friends.png',
      text: '邀请好友',
      url: '/pages/user/invite/invite',
      badge: true
    }, {
      icon: '/resource/images/icon_mine_learn.png',
      text: '学习中心',
      url: '/pages/learn/list',
      badge: false
    }, {
      icon: '/resource/images/icon_mine_feedback.png',
      text: '意见反馈',
      url: '/pages/user/feedback/index',
      badge: false
    }, ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log('app.globalData.userInfo',app.globalData.userInfo)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const isLogin = getApp().globalData.isLogin;
    const isIOS = getApp().globalData.isIOS;
    const roleName = getApp().globalData.roleName;
    const roleId = getApp().globalData.roleId;
    if (isLogin) {
      const {
        avatarUrl,
        nickName,
        companyName,
        mobile,
        vip,
        vipDeadline,
      } = getApp().globalData.userInfo;
      const {
        certificate,
        company,
        invite,
        school,
      } = getApp().globalData.unRead;
      this.setData({
        isIOS,
        isLogin,
        roleName,
        roleId,
        avatarUrl,
        nickName,
        companyName,
        mobile,
        vip,
        vipDeadline,
        'navList[2].badge': certificate.length > 0,
        'navList[3].badge': school.fail.length > 0 || school.pass.length > 0 || school.pending.length > 0,
        'navList[4].badge': company.length > 0,
        'navList[5].badge': invite.members.length > 0 || invite.vips.length > 0,
      })
    } else {
      this.setData({
        isIOS,
        isLogin,
      })
    }

    // this.loadMemberInfo();
  },

  /**
   * 显示会员价格
   */
  loadMemberInfo() {
    if (!this.data.isIOS) {
      getMemberInfo().then(res => {  
        this.setData({
          vipPriceMonth: formatPrice(res.price / 12)
        })
      })
    }
  },

  /**
   * 跳转个人信息
   */
  handleNavigatorToInformation() {
    this.isLogin().then(() => {
      wx.navigateTo({
        url: '/pages/user/information/index',
      })
    })
  },

  /**
   * 跳转会员中心
   */
  handleNavigatorToMember() {
    this.isLogin().then(() => {
      wx.navigateTo({
        url: '/pages/user/member/index',
      })
    })
  },
  onClosed1(){
    this.setData({
      isApply:false
    })
  },
  /**
   * item 的跳转
   * @param {object} e 
   */
  handleNavigatorTo(e) {
    this.isLogin().then(() => {
      if(e.currentTarget.dataset.url=="/pages/user/businessSchool/details"){
        if((this.data.roleId==0 || this.data.roleId==4) || ((this.data.roleId == 1 || this.data.roleId == 2 ||this.data.roleId == 3) && this.data.companyName=="") ){
          this.setData({
            isApply: true,
            showContent: "尊敬的用户，您暂未开通“企业学堂”资格，无法观看系列课程，请联系您的企业负责人为您开通相关权限！",
          })
        }else{
          wx.navigateTo({
            url: e.currentTarget.dataset.url,
          })
        }
      }else if(e.currentTarget.dataset.url=="/pages/user/school/details"){
        console.log(this.data.companyId)
        if(this.data.roleId==4 && this.data.companyName!==""){
          wx.navigateTo({
            url: e.currentTarget.dataset.url,
          })
        }else if(this.data.roleId==0){
          wx.navigateTo({
            url: '/pages/user/school/bind/company',
          })
        }else{
          this.setData({
            isApply: true,
            showContent: "尊敬的用户，您暂未绑定“我的学校”，请联系您的学校负责人为您开通相关权限！",
          })
        }
      }else{
        wx.navigateTo({
          url: e.currentTarget.dataset.url,
        })
      } 
    })
  },

  /**
   * 判断是否登录
   */
  isLogin() {
    return new Promise(resolve => {
      if (this.data.isLogin) {
        resolve()
      } else {
        $wuxDialog().confirm({
          title: '你还没有登陆哦~',
          content: '请先登陆再查看更多内容',
          buttons: [{
            text: '暂不登录',
          }, {
            text: '立即登录',
            type: 'primary',
            onTap(e) {
              wx.navigateTo({
                url: '/pages/login/accredit/accredit',
              })
            }
          }],
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})